//
//  DataParser.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddDataParser.h"
#import "SBJson.h"
#import "JddError.h"
#import "JddUserProfile.h"

@implementation JddDataParser

+(BOOL)isResultSucceeded:(NSDictionary*)dictResult
{
    if (!dictResult) {
        return NO;
    }
    NSInteger code = [[dictResult objectForKey:@"Code"] intValue];
    
//    NSLog(@"code = %d",code);
    
    NSString *Message = [dictResult objectForKey:@"Message"];
    NSString *data = [dictResult objectForKey:@"Data"];
    
    //打印请求出来的数据
//    NSLog(@"data = %@",data);
    
    //区分老系统
    if (Message.length == 0 && data.length > 0) {
        [JddError sharedInstance].message = data;
        return (code == 0 ? YES : NO);
    } else {
        [JddError sharedInstance].message = Message;
        return (code >= 0 ? YES : NO);
    }
}
//
+(BOOL)parseRegisterResult:(NSData*)result
{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return boolResult;
}

+(NSString *)parseLoginResult:(NSData *)result
{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            return realResultString;
        }else {
            return nil;
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
}

+(BOOL)parseGetVerifyCodeRestul:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;    
}

+(NSString *)parseBindMobileResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            NSLog(@"========000000=======x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ")
            return realResultString;
        }else {
            return nil;
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }    
}

+(NSString*)parseLotHallResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            return realResultString;
        }else {
            return nil;
        }
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }

}
//分析用户信息数据
+(BOOL)parseUserInfoResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSDictionary *dictUserInfo = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            
            [JddUserProfile sharedInstance].realName = [dictUserInfo objectForKey:@"RealName"];
            [JddUserProfile sharedInstance].BankName = [dictUserInfo objectForKey:@"BankName"];
            [JddUserProfile sharedInstance].BankCardNumber = [dictUserInfo objectForKey:@"BankCardNumber"];
            [JddUserProfile sharedInstance].IsMobileBind = [dictUserInfo objectForKey:@"IsMobileValied"];
            [JddUserProfile sharedInstance].BankID = [dictUserInfo objectForKey:@"BankID"];
            [JddUserProfile sharedInstance].IDCard = [dictUserInfo objectForKey:@"IDCardNumber"];
            [JddUserProfile sharedInstance].bindMobile = [dictUserInfo objectForKey:@"Mobile"];
            [JddUserProfile sharedInstance].balance = [dictUserInfo objectForKey:@"Balance"];
            [JddUserProfile sharedInstance].IsPayPwd = [dictUserInfo objectForKey:@"IsPayPwd"];
            [JddUserProfile sharedInstance].IsUnion = [[dictUserInfo objectForKey:@"IsUnion"] boolValue];
            [JddUserProfile sharedInstance].BandID = [dictUserInfo objectForKey:@"BindID"];
        }
    }
    @catch (NSException * e) {
      //  NSLog(@"NSException:\t%@", e.reason);incompatible pointer to inteer conversin assigning to 'BOOL'
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;    
}

+(BOOL)parseShopInfoResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSDictionary *dictUserInfo = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            [JddUserProfile sharedInstance].shopID = [dictUserInfo objectForKey:@"LSID"];
            [JddUserProfile sharedInstance].shopLogo = [dictUserInfo objectForKey:@"Logo"];
            [JddUserProfile sharedInstance].lotteries = [dictUserInfo objectForKey:@"Lotteries"];
            [JddUserProfile sharedInstance].shopTitle = [dictUserInfo objectForKey:@"Title"];
            [JddUserProfile sharedInstance].shopPhone = [dictUserInfo objectForKey:@"Mobile"];
            id isGetTicket = [dictUserInfo objectForKey:@"IsGetTicket"];
            if (!isGetTicket || [isGetTicket isKindOfClass:[NSNull class]] || ![isGetTicket boolValue]) {
                [JddUserProfile sharedInstance].isGetTicket = NO;
            } else {
                [JddUserProfile sharedInstance].isGetTicket = YES;
            }
            
        }
    }
    @catch (NSException * e) {
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return boolResult;
}

+ (BOOL)parseTogetherBuyResult:(NSData *)result {
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
    }
    @catch (NSException * e) {
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return boolResult;
}

+(BOOL)parseBankListResult:(NSData*)result bankArray:(NSMutableArray*)bankArray{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSArray *bankListString =  [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            for (NSDictionary *bank in bankListString) {
                [bankArray addObject:bank];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;        
}

+(BOOL)parseBranchBankListResult:(NSData*)result branchBankArray:(NSMutableArray*)branchBankArray{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            if (boolResult) {
                NSArray *bankListString =  [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
                for (NSDictionary *bank in bankListString) {
                    [branchBankArray addObject:bank];
                }
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;        
}

+(NSString*)parseVersionResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    NSString *version = @"";
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        NSArray *dictResult22 = [dictResult objectForKey:@"results"];
        
        for (id config in dictResult22) {
            version = [config valueForKey:@"version"];
        }
        
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return version;
}


+(BOOL)parseProvinceListResult:(NSData*)result provinceArray:(NSMutableArray*)provinceArray{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSArray *provinceListString =  [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            for (NSDictionary *province in provinceListString) {
                [provinceArray addObject:province];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;        
}

+(NSArray*)parseCityListResult:(NSData*)result province:(NSString*)province cityDictionary:(NSMutableArray*)cityDictionary{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            return [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
        }else {
            return nil;
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }    
}

+(BOOL)parseBindCardResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;    
}

+(BOOL)parseModifyPasswordResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;    
}

+(NSString*)parseAccountDetailResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        NSString *useResult = [dictResult objectForKey:@"Data"];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        return useResult;
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
        
}

+(BOOL)parseLotteryIssueResult:(NSData*)result lotteryIssue:(JddLotteryIssue*)lotteryIssue{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            NSDictionary *realDictResult = [[jsonParser objectWithData:[realResultString dataUsingEncoding:NSUTF8StringEncoding]] objectAtIndex:0];
            lotteryIssue.code = [[realDictResult objectForKey:@"IssueID"] stringValue];
            lotteryIssue.name = [realDictResult objectForKey:@"IssueName"];
            lotteryIssue.endTime = [realDictResult objectForKey:@"EndTime"];
            lotteryIssue.openedCount = [[realDictResult objectForKey:@"OpenedCount"] intValue];
            lotteryIssue.remainCount = [[realDictResult objectForKey:@"RemainCount"] intValue];
            lotteryIssue.currentTime = [realDictResult objectForKey:@"CurrentTime"];
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return boolResult;
}

+(NSString*)parseSportLotteryIssueResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            return realResultString;
        }else {
            return nil;
        }
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
}



+(BOOL)parseBuyResult:(NSData*)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return boolResult;
}

+(BOOL)parseDiscountResult:(NSData*)result discountNo:(NSString*)discountNo discountAmount:(NSNumber*)discountAmount{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSDictionary *realResultString = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            discountNo = [realResultString objectForKey:@"VouchersNo"];
            discountAmount = [realResultString objectForKey:@"CanDeductAmount"];
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    
    return boolResult;   
}
    
+(NSString*)parsefollowNumberResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            return realResultString;
        }
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return nil;
}


+(NSString*)parseBuyHistoryResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"];
            return realResultString;
        }
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
}
//解析老接口返回数据 以后去掉
+(NSString*)oldparseBuyHistoryDetailResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Result"];
            return realResultString;
        }else {
            NSString *realResultString = nil;
            return realResultString;
            
        }
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
}

+(NSString*)parseBuyHistoryDetailResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return nil;
    }
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    @try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
            if (boolResult) {
            NSString *realResultString = [dictResult objectForKey:@"Data"]; 
            return realResultString;
        }else {
            NSString *realResultString = nil;
            return realResultString;
        }
    }
    @catch (NSException *e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
}

+(BOOL)paseAlipayInfoResult:(NSData *)result{
    BOOL boolResult = NO;
    if (!result) {
        return boolResult;
    }
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
	@try {
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        boolResult = [JddDataParser isResultSucceeded:dictResult];
        if (boolResult) {
            NSDictionary *realResultString = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            [JddUserProfile sharedInstance].userId = [realResultString objectForKey:@"ID"];
            [JddUserProfile sharedInstance].name = [realResultString objectForKey:@"Name"];
        }
    }
    @catch (NSException * e) {
        NSLog(@"NSException:\t%@", e.reason);
        boolResult = NO;
        [JddError sharedInstance].message = e.reason;
    }
    return boolResult;
}

+(void)parseSportResult:(NSData*)result bankArray:(NSMutableArray*)sportArray{
	SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    NSDictionary *dictResult = [jsonParser objectWithData:result];
    NSDictionary *sportListString =  [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
    if ([sportListString count] > 0) {
        sportListString = [sportListString objectForKey:@"item"];
        for (NSDictionary *sport in sportListString) {
            [sportArray addObject:sport];
        }
    }
}

@end

